package edu.tgc.entity.log;

import java.util.Date;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.aspectj.lang.JoinPoint;
import org.springframework.data.annotation.CreatedDate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import edu.tgc.entity.system.BaseEntity;
import edu.tgc.entity.system.SysUser;
import edu.tgc.security.SessionUserUtils;

@Entity
@Table(indexes = { @Index(columnList = "success asc"), @Index(columnList = "operate asc"), @Index(columnList = "createTime asc") })
public class LogMethod extends BaseEntity {

    private static final long serialVersionUID = -4392991579439042656L;

    public static LogMethod failed(SysUser sysUser, String operate, String message, JoinPoint joinPoint, String parameter) {
        return new LogMethod(false, operate, message, sysUser, SessionUserUtils.getIp(), parameter, null, joinPoint.getTarget().getClass().toString(), joinPoint.getSignature().getName());
    }

    public static LogMethod success(SysUser sysUser, String operate, JoinPoint joinPoint, String parameter, String result) {
        return new LogMethod(true, operate, null, sysUser, SessionUserUtils.getIp(), parameter, result, joinPoint.getTarget().getClass().toString(), joinPoint.getSignature().getName());
    }

    protected String callerClass;

    protected String callerMethod;
    protected Date createTime;
    protected String ip;

    protected String message;
    protected String operate;
    protected String parameter;
    protected String result;

    protected Boolean success = true;

    @JsonIgnoreProperties(value = { "createUser", "lastModifyUser", "createTime", "lastModifyTime", "dept", "description", "enabled", "ip", "username", "sort", "address", "dutyStatus", "education", "email", "hireDate", "idNo", "leaveDate", "major",
            "qq", "sex", "status", "telphone" })
    private SysUser user;

    public LogMethod() {
    }

    public LogMethod(Boolean success, String operate, String message, SysUser user, String ip, String parameter, String result, String callerClass, String callerMethod) {
        this.success = success;
        this.operate = operate;
        this.message = message;
        this.user = user;
        this.ip = ip;
        this.parameter = parameter;
        this.result = result;
        this.callerClass = callerClass;
        this.callerMethod = callerMethod;
    }

    public LogMethod(UUID id) {
        this.id = id;
    }

    @Column(length = 255)
    public String getCallerClass() {
        return this.callerClass;
    }

    @Column(length = 255)
    public String getCallerMethod() {
        return this.callerMethod;
    }

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreateTime() {
        return this.createTime;
    }

    @Column(length = 39)
    public String getIp() {
        return this.ip;
    }

    @Column(length = 255)
    public String getMessage() {
        return this.message;
    }

    @Column(length = 31)
    public String getOperate() {
        return this.operate;
    }

    @Column(length = 4096)
    public String getParameter() {
        return this.parameter;
    }

    @Column(length = 4096)
    public String getResult() {
        return this.result;
    }

    public Boolean getSuccess() {
        return this.success;
    }

    @ManyToOne
    public SysUser getUser() {
        return this.user;
    }

    public void setCallerClass(String callerClass) {
        this.callerClass = callerClass;
    }

    public void setCallerMethod(String callerMethod) {
        this.callerMethod = callerMethod;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public void setUser(SysUser user) {
        this.user = user;
    }

}
